﻿using System.Collections.Generic;
using System.Linq;
using Missbot.Core.Cache;
using Missbot.Model.MsgCmds.Admin;
using Missbot.Model.MsgCmds.Drive;
using Missbot.Model.MsgCmds.Notification;
using Missbot.Model.MsgCmds.User;
using Missbot.Model.MsgInfos;

namespace Missbot.Core.Common
{
    /// <summary>
    /// 实例接口请求器
    /// </summary>
    public class Requestor
    {
        #region 用户相关

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public static InstanceUser GetUserByUserName(string UserName)
        {
            var sps = UserName.Split('@');
            return sps.Length == 1 ? GetUserByUserName(UserName, null) : GetUserByUserName(sps[0], sps[1]);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Host"></param>
        /// <returns></returns>
        public static InstanceUser GetUserByUserName(string UserName, string Host)
        {
            return MsgSender.PushMsgWithResp<InstanceUser>(new GetUserByUsernameCmd { UserName = UserName, Host = Host });
        }

        #endregion

        #region 网盘

        /// <summary>
        /// 获取文件夹中的文件列表
        /// </summary>
        /// <param name="folderID"></param>
        /// <returns></returns>
        public static IEnumerable<InstanceFile> GetFolderFiles(string folderID)
        {
            var files = MsgSender.PushMsgWithResp<List<InstanceFile>>(new GetFolderFilesCmd { FolderID = folderID, Limit = 100 });
            var curCount = files.Count;
            while (curCount > 0)
            {
                var cmd = new GetFolderFilesPagedCmd { FolderID = folderID, Limit = 100, UntilID = files.Last().ID };
                var resp = MsgSender.PushMsgWithResp<List<InstanceFile>>(cmd);
                curCount = resp.Count;
                files.AddRange(resp.Where(r => files.All(f => f.ID != r.ID)));
            }

            return files;
        }

        /// <summary>
        /// 获取子文件夹信息
        /// </summary>
        /// <param name="parentFolderID"></param>
        /// <returns></returns>
        public static IEnumerable<InstanceFolder> GetFolders(string parentFolderID = null)
        {
            var folders = MsgSender.PushMsgWithResp<List<InstanceFolder>>(new GetFoldersCmd { FolderID = parentFolderID, Limit = 100 });
            var curCount = folders.Count;
            while (curCount > 0)
            {
                var cmd = new GetFoldersPagedCmd { FolderID = parentFolderID, Limit = 100, UntilID = folders.Last().ID };
                var resp = MsgSender.PushMsgWithResp<List<InstanceFolder>>(cmd);
                curCount = resp.Count;
                folders.AddRange(resp.Where(r => folders.All(f => f.ID != r.ID)));
            }

            return folders;
        }

        /// <summary>
        /// 获取网盘文件夹信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static InstanceFolder CreateFolder(string name)
        {
            return MsgSender.PushMsgWithResp<InstanceFolder>(new CreateRootFolderCmd { Name = name });
        }

        #endregion

        #region 实例基础信息

        /// <summary>
        /// 获取实例状态
        /// </summary>
        /// <returns></returns>
        public static InstanceStatus GetInstanceStatus()
        {
            return MsgSender.PushMsgWithResp<InstanceStatus>(new GetStatsCmd());
        }

        #endregion

        #region 群组

        /// <summary>
        /// 获取已经加入的群组列表
        /// </summary>
        /// <returns></returns>
        public static JoinedGroupInfo[] GetJoinedGroups()
        {
            return MsgSender.PushMsgWithResp<JoinedGroupInfo[]>(new JoinedGroupCmd());
        }

        #endregion
    }
}